package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.NoticeDTO;
import com.quanquan.store.dto.SquareDTO;
import com.quanquan.store.entity.Notice;
import com.quanquan.store.request.NoticeRequest;
import com.quanquan.store.request.SquareRequest;
import com.quanquan.store.service.SquareService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

import java.util.List;
import java.util.Map;

/**
 * @ClassName:SquareController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/1 14:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/square")
public class SquareController {

    public static final String DOWN_SUCCESS = "下架成功";
    public static final String UP_SUCCESS = "上架成功";
    private final SquareService squareService;

    @Autowired
    public SquareController(SquareService squareService) {
        this.squareService = squareService;
    }

    /**
     * 配置商城优惠券可兑换数量、修改
     *
     * @param request
     * @return
     */
    @PostMapping("/add_count")
    public ResultBody addCount(@RequestBody SquareRequest request) {
        boolean flag = squareService.saveSquare(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", flag);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 商城兑换笔数限制列表
     *
     * @return
     */
    @PostMapping("/list")
    public ResultBody list() {
        List<SquareDTO> squareDTO = squareService.list();
        if (squareDTO.size()<=0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", squareDTO);
    }

    @PostMapping("/find_by_id")
    public ResultBody findById() {
        SquareDTO squareDTO = squareService.findById();
        if (squareDTO==null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", squareDTO);
    }

    /**
     * 添加公告
     *
     * @param request
     * @return
     */
    @PostMapping("/add_notice")
    @ResponseBody
    public ResultBody saveNotice(@RequestBody NoticeRequest request) {
        boolean flag = squareService.saveNotice(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 修改公告
     *
     * @param request
     * @return
     */
    @PostMapping("/update_notice")
    public ResultBody updateNotice(@RequestBody NoticeRequest request) {
        boolean flag = squareService.updateNotice(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 修改公告
     *
     * @param request
     * @return
     */
    @PostMapping("/update_status")
    public ResultBody updateStatus(@RequestBody NoticeRequest request) {
        boolean flag = squareService.updateStatus(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }


    /**
     * 删除公告
     *
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public ResultBody delete(@RequestBody NoticeRequest request) {
        boolean flag = squareService.delete(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }


    /**
     * 查询公告列表
     *
     * @param request
     * @return
     */
    @PostMapping("/find_by_status")
    public ResultBody findByStatus(@RequestBody NoticeRequest request) {
        List<Object> objectList = squareService.findByStatus(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 查看公告详情
     *
     * @param request
     * @return
     */
    @PostMapping("/find_by_notice_id")
    public ResultBody findByNoticeId(@RequestBody NoticeRequest request) {
        Map<String, Object> map = squareService.findByNoticeId(request);
        if (map == null || map.isEmpty()) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", map);
    }

    /**
     * 查看公告详情
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id_manage")
    public ResultBody queryByIdManage(@RequestBody NoticeRequest request) {
        NoticeDTO noticeDTO = squareService.queryMenegement(request);
        if (noticeDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", noticeDTO);
    }


    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("query_select")
    public ResultBody queryByCondition(@RequestBody NoticeRequest request) {
        PageDTO<Notice> noticePageDTO;
        try {
            noticePageDTO = squareService.queryByCondition(request);
            if (null != noticePageDTO && noticePageDTO.getList().size() > 0) {
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), noticePageDTO);
            }
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        } catch (RuntimeException e) {
            throw new RuntimeException("查询异常");
        }
    }


}
